//var _url = "http://172.21.201.144:8080/ipl-restapi/restapi/";
var layer;
var form;
layui.use(['layer', 'form'], function(){
    layer = layui.layer
        ,form = layui.form;


});
var _url = "json/";
var sq = {
    url : _url,
    _data: {},
    _your_api : "",
    _fun : function () {
      console.log("没有传入处理函数");
    },
    _error : function () {
      console.log("出错了，但是没有传入错误处理函数");
    },
    setdata : function (js_obj) {
        this._data = js_obj;
        return this;
    },
    setapi : function (your_api) {
        this._your_api = your_api;
        return this;
    },
    sucessFun : function (your_fun) {
        this._fun = your_fun;
        return this;
    },
    errorFun : function (your_fun) {
        this._error = your_fun;
        return this;
    },
    run : function () {
        $.ajax({
            url: this.url+this._your_api,
            type : "GET",
            data : this._data,
            async:true,
            dataType : "JSON",
            xhrFields   : {withCredentials: true},
            success: function(result){
               // console.log(this._data+"返回的结果是："+JSON.stringify(result));
                switch (parseInt(result.status)){
                    case 60 :
                        layer.prompt({title: result.src, formType: 1}, function(pass, index){
                                layer.close(index);
                                //console.log(pass);
                            sq.setapi("handle_nosession.php").setdata({"type":"loginin","password":pass})
                                .sucessFun(function (result) {
                                    //console.log(result);
                                    sq.setapi("handle.php").setdata({"type":"getrolelist"})
                                        .sucessFun(function (result) {
                                            var myobj = result.ddlist;
                                            for(var i = 0;i<myobj.length;i++){
                                                $("#switcontent").html($("#switcontent").html()+'<li onclick="openchild('+myobj[i][0]+')">'+myobj[i][1]+'</li>');
                                            }
                                        }).run();
                                }).run();
                        });
                        break;
                    case 0 :
                        console.log(11111);
                        sq._fun(result);
                        break;
                    default :
                        //console.log(result);
                        console.log("未知status！");
                        break;

                }
            },
            error:function (xhr){
                //console.log(xhr);
                sq._error();
            }
        })
    },
    openDataPage :function (s,title) {
        layer.open({
            type: 2,
            skin: 'layui-layer-molv',
            title: title,
            shadeClose: true,
            shade: 0.618,
            maxmin: true, //开启最大化最小化按钮
            area: ['1200px', '800px'],
            content: 'sun/'+s+".html"
        });
    },
    open :function (s){
        window.location.href = s;
    }
};
